Zepto(function($) {
  var modelgoodsId =  sessionStorage.getItem("cateId");
  var brandGoodId = sessionStorage.getItem("brandGoodId");
  var specificationId = "";
  var num = 1;
  var toComfire;//是否加入购物车
  window.onload=function () {
    getGoodsDetails();
  }
  /**
   * 查询商品详情
   *
   */
  function getGoodsDetails() {
    $.ajax({
      type: 'GET',
      url: getUrl + '/wxmall/goods/selectGoodsDetail',
      data: {
        "goodsId":brandGoodId
      },
      dataType: 'json',
      timeout: 15000,
      headers: headers("goods.selectGoodsDetail"),
      context: $('body'),
      success: function(data){
        getGoodsDetailsInit(data);
      },
      error: function(xhr, type){
        // alert('Ajax error!')
      }
    });
  }
  /**
   * 商品详情参数初始化
   *
   * @param data
   */
  function getGoodsDetailsInit(data) {
    console.log(data);
    var data1 = data.data;
    if(!data.success){
      return;
    }
    if(data1 == null || data1 ==undefined || data1 == ""){
      return;
    }
    if(data1.attribute != null && data1.attribute != "" && data1.attribute != undefined){
      var array = data1.attribute.split(",");
      if(array.length < 0){
        return
      }
      $("#attribute").html("");
      var html = "";
      for(var i = 0 ; i < array.length; i++){
        if(array[i] !=""){
          html += '<div class="yhdeta-eve">'+
              '<div class="yhdeta-eve-img"></div>'+
              '<span>' + array[i] + '</span>'+
              '</div>';
        }
      }
      $("#attribute").append(html);
    }
    $("#goodsName").html(data1.goodsName);
    // $("#specification").html(data.data.specification);
    $("#wechat_price").html(data1.wechat_price);
    $("#limitPrice").html(data1.limitPrice);
    $("#goodsName").html(data1.goodsName);
    if(data1.freight == "00"){
      $("#postage").html("免运费");
    }
    if(data1.goodsImage != null){
      if(data1.goodsImage.length != 0){
        $("#bannerPic").html("");
        var bannerHtml= "";
        for(var i = 0 ; i < data1.goodsImage.length ; i++){
          var bannerImg = "";
          if(data1.goodsImage[i].picUrl != null){
            bannerImg = imgUrl +  data1.goodsImage[i].picUrl;
          }
         if(data1.goodsImage[i].picType == 1){
           bannerHtml += '<div class="swiper-slide">' +
                '<img src="'+ bannerImg +'">' +
                '</div>';
           $("#cartHeaderImg").src= bannerImg;
         }
        }
        for(var i = 0 ; i < data1.goodsImage.length ; i++){
          var bannerImg = "";
          if(data1.goodsImage[i].picUrl != null){
            bannerImg = imgUrl +  data1.goodsImage[i].picUrl;
          }
          if(data1.goodsImage[i].picType != 1){
            bannerHtml += '<div class="swiper-slide">' +
                '<img src="'+ bannerImg +'">' +
                '</div>';
            $("#cartHeaderImg").src= bannerImg;
          }
        }
        $("#bannerPic").append(bannerHtml);
      }
      /**
       * 初始化banner 图
       * @type {Window.Swiper|*}
       */
      var mySwiper = new Swiper('.swiper-container', {
        loop: false,
        autoplay: 3000,
        pagination: '.swiper-pagination'
      });
    }
    /**
     * 产品参数
     *
     */
    var goodsJsonStr = JSON.parse(data1.goodsJsonStr);
    if(goodsJsonStr.sepc != null && goodsJsonStr.sepc != undefined && goodsJsonStr.sepc != ''){
      var packJson = goodsJsonStr.sepc;
      $("#goodsMsg").html("");
      var html = "";
      for(var p in packJson){
        if(p != ""){
          html += '<li>' +
              '<span class="title-type">'+ p +'</span>' +
              '<span>'+packJson[p]+'</span>' +
              '</li>';
        }
      }
      $("#goodsMsg").append(html);
      var detailspic = goodsJsonStr.picdetail
      $(".details-img").append('<img src="'+ imgUrl + detailspic + '">');
    }
  }
  /**
   * 获取规格列表
   *
   */
  function getTypeListByGoodsId() {
    $.ajax({
      type: 'GET',
      url: getUrl + '/wxmall/specification/selectListByGoodsId',
      data: {
        "goodsId":brandGoodId
      },
      dataType: 'json',
      timeout: 15000,
      headers: headers("specification.selectListByGoodsId"),
      context: $('body'),
      success: function(data){
        getTypeInit(data);
      },
      error: function(xhr, type){
        console.log(xhr,type,"hhsgdaga");
      }
    });
  }
  /**
   * 初始化规格列表
   *
   * @param data
   */
  function getTypeInit(data) {
    console.log(data);
    var data1 = data.data;
    if(data1 != null && data1.length != 0 ){
      $("#typeList").html("");
      var html = "" ;
      for(var i = 0 ; i < data1.length ; i ++ ){
        html += '<div data-id="' + data1[i].specificationId + '" data-price="'+data1[i].wechatPrice+'" class="specifi-type-every">'+ data1[i].specification + '</div>';
      }
      $("#typeList").append(html);
    }
  }
  /**
   * 加入购物车
   *
   */
  function tocart() {
    var data1 = {
      "specificationId":specificationId,
      "num":num
    }
    $.ajax({
      type: 'POST',
      url: getUrl + '/wxmall/shopcarts/insert',
      data: data1,
      dataType: 'json',
      timeout: 15000,
      // contentType:'application/json',
      headers: headers("shopcarts.insert"),
      context: $('body'),
      success: function(data){
        tocartbefore(data);
      },
      error: function(xhr, type, data){
        console.log(xhr,type,data);
      }
    });
  }
  /**
   * 加入购物车之后处理
   *
   * @param data
   */
  function tocartbefore(data) {
    //为true 立即购买
    if(toComfire){
      if(data.success){
        var ShopCartsIdList = [];
        ShopCartsIdList.push(data.data);
        sessionStorage.setItem("ShopCartsIdList",JSON.stringify(ShopCartsIdList));
        $(".model").css("display","none");
        window.location.href="/usercenter/order/confirmOrder.html"
      }else {
        $(".model").css("display","none");
        mui.toast(data.msg);
      }
    }else {
      if(data.success){
        $(".model").css("display","none");
        mui.toast("已加入购物车");
      }else {
        $(".model").css("display","none");
        mui.toast(data.msg);
      }
    }

  }

  /**
   * 返回上一页
   *
   */
  $(".nav-header").on("click",".back",function (e) {
    window.history.go(-1);
  });
  /**
   * 弹出框
   *
   * 加入购物车
   */
  $(".operate-content").on("click",".operate-addcart",function () {
    $(".model").css("display","block");
    $(".cart-footer").html("加入购物车");
    getTypeListByGoodsId();
  });
  /**
   * 立即购买
   */
  $(".operate-content").on("click",".operate-buynow",function () {
    $(".model").css("display","block");
    $(".cart-footer").html("立即购买");
    getTypeListByGoodsId();
  });
  /**
   * 关闭弹出框
   *
   */
  $(".cart-header").on("click",".close-icon",function () {
    $(".model").css("display","none");
  });
  /**
   * 减数量
   *
   */
  $(".select-num-content").on("click",".add-num",function () {
    console.log(num);
    if(num > 1){
      num -= 1;
      $(".value-num").html(num);
    }
  });
  /**
   * 加数量
   *
   */
  $(".select-num-content").on("click",".add-add",function () {
    num += 1;
    $(".value-num").html(num);
  });
  /**
   * 选择规格
   *
   */
  $(".specifi-type").on("click",".specifi-type-every",function () {
    $(".specifi-type-every").removeClass("active");
    $(this).addClass("active");
    console.log($(this)[0].getAttribute("data-price"));
    $("#goodsPrice").html("￥" + $(this)[0].getAttribute("data-price"));
    $("#speciType").html($(this)[0].innerHTML);
    specificationId = $(this)[0].getAttribute("data-id");
  });
  /**
   * 加入购物车或者立即购买
   *
   * @type {number}
   */
  $(".cart-footer").click(function () {
    if($(this)[0].innerText == "加入购物车"){
      toComfire = false;
      if(specificationId == "") mui.toast("请选择规格");else tocart();
    }else{
      toComfire = true;
      if(specificationId == "") mui.toast("请选择规格");else tocart();
    }
  });
  /**
   * 跳转到购物车
   * @type {number}
   */
  $("#toCart").click(function () {
    window.location.href = "/usercenter/cart.html";
  });
  $("body").screenTop = 0;
});